/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.joyz.api.result.util;

/**
 * @author JoyZ
 */
public class ObjectUtils {

    /**
     * 检查给定数组 array 是否非空且长度大于0.
     *
     * @param array 要检查的数组
     * @return 如果满足条件返回 {@code true}, 反之则返回 {@code false}
     */
    public static boolean isEmpty(Object[] array) {
        return (array == null || array.length == 0);
    }

    /**
     * 检查给定 text 是否为有效文本.
     * <p>文本必须不能为 {@code null}, 非空, 且包含至少一个非空白字符
     *
     * @param text 要检查的文本
     * @return 如果满足条件返回 {@code true}, 反之则返回 {@code false}
     */
    public static boolean hasText(String text) {
        return (text != null && !text.isEmpty() && containsText(text));
    }

    /**
     * 检查给定 text 是否为空白字符.
     *
     * @param text 要检查的文本
     * @return 如果满足条件返回 {@code true}, 反之则返回 {@code false}
     */
    public static boolean containsText(CharSequence text) {
        int textLength = text.length();
        for (int i = 0; i < textLength; i++) {
            if (!Character.isWhitespace(text.charAt(i))) {
                return true;
            }
        }
        return false;
    }
}
